#!/bin/bash
# sudochange.sh does the actuall change on the host 
# Copyright (C) 2008 Rene Geers (rene.geers@gmail.com) and Bjoern Remseth (la3lma@gmail.com)
# 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

#Search for a string from the changefile
if [ ! -z "`grep "backup-script" /etc/sudoers`" ];then
   echo "Allready installed"
   exit 0
fi

# Do the actuall change
cat /etc/sudoers > /tmp/sudoers
echo "#Part of backup-script" >> /tmp/sudoers
echo "User_Alias      HALTERS = rene, backup" >> /tmp/sudoers
echo "Cmnd_Alias      HALT = /sbin/halt" >> /tmp/sudoers
echo "Cmnd_Alias      SHUTDOWN = /sbin/shutdown" >> /tmp/sudoers
echo "HALTERS ALL = NOPASSWD: HALT" >> /tmp/sudoers
visudo -c -f /tmp/sudoers
if [ $? != 0 ]; then 
   rm /tmp/sudoers
   exit 1
fi
if [ -e /etc/sudoers.tmp ]; then 
   rm /etc/sudoers.tmp
   exit 1
fi
mv /tmp/sudoers /etc/sudoers
chmod 440 /etc/sudoers
